<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class MaintainModel extends Model
{
    protected $table = 'maintains';

    public $timestamps = false;

    protected $appends = ['content_text', 'type_text', 'status_text'];

    /**
     * Notes: 时间格式转换
     * User: lstink
     * Date: 2021/2/21
     * Time: 3:13 下午
     * @param $value
     * @return false|string
     */
    public function getCreatedAtAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * Notes: 截取内容
     * User: lstink
     * Date: 2021/2/21
     * Time: 4:34 下午
     * @return mixed|string
     */
    public function getContentTextAttribute()
    {
        if (mb_strlen($this->content) <= 10) {
            return $this->content;
        }

        return mb_substr($this->content, 0 , 10) . '...';
    }

    /**
     * Notes: 图片解析
     * User: lstink
     * Date: 2021/2/22
     * Time: 9:42 上午
     * @param $value
     * @return false|string[]
     */
    public function getImgAttribute($value)
    {
        $arr = explode(',', $value);
        return $arr;
    }

    /**
     * Notes: 获取报修类型
     * User: lstink
     * Date: 2021/2/22
     * Time: 10:11 上午
     * @return string
     */
    public function getTypeTextAttribute()
    {
        if ($this->type == 1) {
            return '公共报修';
        }
        if ($this->type == 2) {
            return '房屋报修';
        }

        return '其它';

    }

    /**
     * Notes: 获取状态描述
     * User: lstink
     * Date: 2021/3/4
     * Time: 6:40 下午
     * @return string
     */
    public function getStatusTextAttribute()
    {
        if ($this->status == 0) {
            return '待处理';
        }
        if ($this->status == 1) {
            return '处理中';
        }
        if ($this->status == 2) {
            return '已完成';
        }

        return '其它';
    }

    /**
     * Notes: 关联小区
     * User: lstink
     * Date: 2021/2/2
     * Time: 5:17 下午
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function community()
    {
        return $this->belongsTo(Community::class, 'c_id', 'community_id');
    }

    /**
     * Notes: 关联物业
     * User: lstink
     * Date: 2021/2/2
     * Time: 5:17 下午
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function estate()
    {
        return $this->belongsTo(Property::class, 'e_id', 'id');
    }

    /**
     * Notes: 关联用户表
     * User: lstink
     * Date: 2021/2/21
     * Time: 11:52 上午
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }
}
